package com.fitness.fitness.controller;

import cn.hutool.json.JSONUtil;
import com.fitness.fitness.modal.datavisual.entity.*;
import com.fitness.fitness.modal.response.ServerResponseEntity;
import com.fitness.fitness.service.IDataVisualService;
import com.fitness.fitness.ws.Message;
import com.fitness.fitness.ws.WebSocketServer;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/data-visual")
public class DataVisualController {

    private final IDataVisualService dataVisualService;

//    @Scheduled(cron = "0/10 * * * * ?")
    @GetMapping(value = "/getRateVisuals")
    public ServerResponseEntity<List<RateVisual>> getRateVisuals() throws Exception {
        List<RateVisual> rateVisuals = dataVisualService.getRateVisuals();
        Message<List<RateVisual>> message = new Message<>();
        message.setType("rateVisual");
        message.setContent(rateVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
        return ServerResponseEntity.success(rateVisuals);
    }

//    @Scheduled(cron = "0/10 * * * * ?")
    @GetMapping(value = "/getPriceVisuals")
    public ServerResponseEntity<List<PriceVisual>> getPriceVisuals() throws Exception {
        List<PriceVisual> priceVisuals = dataVisualService.getPriceVisuals();
        Message<List<PriceVisual>> message = new Message<>();
        message.setType("priceVisual");
        message.setContent(priceVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
        return ServerResponseEntity.success(priceVisuals);
    }

//    @Scheduled(cron = "0/10 * * * * ?")
    @GetMapping(value = "/getCategoryVisuals")
    public ServerResponseEntity<List<CategoryVisual>> getCategoryVisuals() throws Exception {
        List<CategoryVisual> categoryVisuals = dataVisualService.getCategoryVisuals();
        Message<List<CategoryVisual>> message = new Message<>();
        message.setType("categoryVisual");
        message.setContent(categoryVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
        return ServerResponseEntity.success(categoryVisuals);
    }

//    @Scheduled(cron = "0/10 * * * * ?")
    @GetMapping(value = "/getGenderVisuals")
    public ServerResponseEntity<List<GenderVisual>> getGenderVisuals() throws Exception {
        List<GenderVisual> genderVisuals = dataVisualService.getGenderVisuals();
        Message<List<GenderVisual>> message = new Message<>();
        message.setType("genderVisual");
        message.setContent(genderVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
        return ServerResponseEntity.success(genderVisuals);
    }

//    @Scheduled(cron = "0/10 * * * * ?")
    @GetMapping(value = "/getAgeVisuals")
    public ServerResponseEntity<List<AgeVisual>> getAgeVisuals() throws Exception {
        List<AgeVisual> ageVisuals = dataVisualService.getAgeVisuals();
        Message<List<AgeVisual>> message = new Message<>();
        message.setType("ageVisual");
        message.setContent(ageVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
        return ServerResponseEntity.success(ageVisuals);
    }

//    @Scheduled(cron = "0/10 * * * * ?")
    @GetMapping(value = "/getCourseTopNVisuals")
    public ServerResponseEntity<List<CourseTopNVisual>> getCourseTopNVisuals() throws Exception {
        List<CourseTopNVisual> courseTopNVisuals = dataVisualService.getCourseTopNVisuals();
        Message<List<CourseTopNVisual>> message = new Message<>();
        message.setType("ageVisual");
        message.setContent(courseTopNVisuals);
        WebSocketServer.sendAllMessage(JSONUtil.toJsonStr(message));
        return ServerResponseEntity.success(courseTopNVisuals);
    }

}
